<template>
	<view class="recordings-container">
		<view class="header">
			<text class="title">我的录音</text>
		</view>
		
		<view class="empty-state" v-if="recordings.length === 0 && !loading">
			<view class="empty-icon">
				<text class="icon-text">🎙️</text>
			</view>
			<text class="empty-text">暂无录音记录</text>
		</view>
		
		<view class="loading" v-if="loading">
			<uni-load-more status="loading" :contentText="loadingText"></uni-load-more>
		</view>
		
		<view class="recording-list" v-if="recordings.length > 0">
			<view class="recording-item" v-for="(item, index) in recordings" :key="item.id" @click="playRecording(item)">
				<view class="recording-info">
					<text class="recording-title">{{item.sentence_content}}</text>
					<text class="recording-date">{{formatDate(item.created_at)}}</text>
				</view>
				
				<view class="recording-actions">
					<view class="action-btn play" :class="{'playing': currentPlaying === item.id}" @click.stop="playRecording(item)">
						<text class="btn-icon">{{currentPlaying === item.id ? '■' : '▶'}}</text>
					</view>
					<view class="action-btn delete" @click.stop="confirmDelete(item)">
						<text class="btn-icon">🗑️</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { recordingAPI } from '../../utils/api.js';
	
	export default {
		data() {
			return {
				recordings: [],
				currentPlaying: null,
				audioContext: null,
				loading: false,
				loadingText: {
					contentdown: '加载更多',
					contentrefresh: '加载中...',
					contentnomore: '没有更多数据了'
				}
			}
		},
		onLoad() {
			this.getRecordings();
			this.audioContext = uni.createInnerAudioContext();
			this.audioContext.onEnded(() => {
				this.currentPlaying = null;
			});
			this.audioContext.onError((res) => {
				console.error('播放录音失败：', res);
				uni.showToast({
					title: '播放失败',
					icon: 'none'
				});
				this.currentPlaying = null;
			});
		},
		onUnload() {
			if (this.audioContext) {
				this.audioContext.destroy();
			}
		},
		methods: {
			getRecordings() {
				this.loading = true;
				
				// 调用API获取用户录音列表
				recordingAPI.getUserRecordings()
					.then(res => {
						if (res.code === 200) {
							this.recordings = res.data || [];
						} else {
							uni.showToast({
								title: res.message || '获取录音列表失败',
								icon: 'none'
							});
						}
					})
					.catch(err => {
						console.error('获取录音列表失败', err);
						uni.showToast({
							title: '获取录音列表失败',
							icon: 'none'
						});
					})
					.finally(() => {
						this.loading = false;
					});
			},
			playRecording(item) {
				if (this.currentPlaying === item.id) {
					// 如果点击的是当前正在播放的录音，则停止播放
					this.audioContext.stop();
					this.currentPlaying = null;
				} else {
					// 停止当前播放的录音
					if (this.currentPlaying) {
						this.audioContext.stop();
					}
					
					// 播放选中的录音
					const audioUrl = recordingAPI.getRecordingUrl(item.id);
					this.audioContext.src = audioUrl;
					this.audioContext.play();
					this.currentPlaying = item.id;
				}
			},
			confirmDelete(item) {
				uni.showModal({
					title: '确认删除',
					content: '确定要删除这条录音吗？',
					success: (res) => {
						if (res.confirm) {
							this.deleteRecording(item);
						}
					}
				});
			},
			deleteRecording(item) {
				// 如果正在播放要删除的录音，先停止播放
				if (this.currentPlaying === item.id) {
					this.audioContext.stop();
					this.currentPlaying = null;
				}
				
				uni.showLoading({
					title: '删除中...'
				});
				
				// 调用API删除录音
				recordingAPI.deleteRecording(item.id)
					.then(res => {
						if (res.code === 200) {
							// 从列表中删除录音
							const index = this.recordings.findIndex(recording => recording.id === item.id);
							if (index !== -1) {
								this.recordings.splice(index, 1);
							}
							
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							});
						} else {
							uni.showToast({
								title: res.message || '删除失败',
								icon: 'none'
							});
						}
					})
					.catch(err => {
						console.error('删除录音失败', err);
						uni.showToast({
							title: '删除失败',
							icon: 'none'
						});
					})
					.finally(() => {
						uni.hideLoading();
					});
			},
			formatDate(dateString) {
				if (!dateString) return '';
				
				const date = new Date(dateString);
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				const hours = String(date.getHours()).padStart(2, '0');
				const minutes = String(date.getMinutes()).padStart(2, '0');
				
				return `${year}-${month}-${day} ${hours}:${minutes}`;
			}
		}
	}
</script>

<style lang="scss">
	.recordings-container {
		min-height: 100vh;
		background-color: #D3E0F3;
		padding: 30rpx;
	}
	
	.header {
		margin-bottom: 30rpx;
		
		.title {
			font-size: 36rpx;
			color: #7397AB;
			font-weight: bold;
		}
	}
	
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-top: 200rpx;
		
		.empty-icon {
			width: 200rpx;
			height: 200rpx;
			margin-bottom: 30rpx;
		}
		
		.empty-text {
			font-size: 32rpx;
			color: #999;
		}
	}
	
	.recording-list {
		.recording-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: rgba(255, 255, 255, 0.95);
			border-radius: 20rpx;
			padding: 30rpx;
			margin-bottom: 20rpx;
			border: 3rpx solid #D1D9E0;
			
			.recording-info {
				flex: 1;
				margin-right: 20rpx;
				
				.recording-title {
					font-size: 28rpx;
					color: #333;
					margin-bottom: 10rpx;
				}
				
				.recording-date {
					font-size: 24rpx;
					color: #999;
				}
			}
			
			.recording-actions {
				display: flex;
				
				.action-btn {
					width: 70rpx;
					height: 70rpx;
					border-radius: 35rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					margin-left: 15rpx;
					
					.btn-icon {
						font-size: 32rpx;
					}
				}
				
				.play {
					background-color: #7397AB;
					color: #fff;
					
					&.playing {
						background-color: #A1AFC9;
					}
				}
				
				.delete {
					background-color: #f5f5f5;
					color: #ff5a5f;
				}
			}
		}
	}
</style> 